﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using E_Shop.Common;
using ESHOP.DAO;
using ESHOP.UTIL;

namespace E_Shop
{
    public partial class Login : Authentication
    {
        private readonly AccountDAO _accountDao = new AccountDAO();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private bool CheckLogin(string user, string pass)
        {
            var account = _accountDao.CheckLogin(user, pass);
            if (account != null)
            {
                Session[Constants.SESSION_LOGIN] = account;
                return true;
            }
            return false;
        }

        protected void btnlogin_Click(object sender, EventArgs e)
        {
            String user = txtuser.Text.Trim();
            String pass = txtpass.Text.Trim();
            if (CheckLogin(user, pass))
            {
                if (Session[Constants.SESSION_URLLATE] != null)
                {
                    Response.Redirect(Session[Constants.SESSION_URLLATE].ToString(), true);
                }
                else
                {
                    Response.Redirect(WebUrl.BUSINESS_HOME, true);
                }
            }
            else { lberror.Text = "username and password is invalid"; }
        }
    }
}